<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>首页 - 华鑫农业后台管理系统后台管理系统</title>
<link rel="icon" href="favicon.ico" type="image/ico">
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/materialdesignicons.min.css" rel="stylesheet">
<link href="../css/style.min.css" rel="stylesheet">
  <script src="../js/myScript/app.js"></script>
  <script type="text/javascript" src="../js/jquery.min.js"></script>

  <script type="text/javascript" src="../js/bootstrap.min.js"></script>
  <script type="text/javascript" src="../js/perfect-scrollbar.min.js"></script>
  <script type="text/javascript" src="../js/main.min.js"></script>
<%--  <script type="text/javascript" src="../js/template-web.js"></script>--%>
</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <%--    菜单栏--%>
    <jsp:include page="left_nav.jsp"/>
    <%--头部--%>
    <jsp:include page="header.jsp"/>
      <script type="text/javascript">

        for (let i = 1; i <= 6; i++) {
          $("#nav" + i).attr("class", "nav-item");
        }
        $("#nav4").attr("class", "nav-item active");
      </script>

      <!--页面主要内容-->
      <main class="lyear-layout-content">

        <div class="container-fluid">

          <div class="row">
            <div class="col-lg-12">
              <div class="card">
                <div class="card-toolbar clearfix">

                  <%--                搜索功能的页面--%>
                  <form class="pull-right search-bar" method="get" action="#!" role="form">
                    <div class="input-group">
                      <div class="input-group-btn">
                        <input type="hidden" name="search_field" id="search-field" value="title">
                        <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                          内容 <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu">
                          <li> <a tabindex="-1" href="javascript:void(0)" data-field="title">内容</a> </li>
                          <li> <a tabindex="-1" href="javascript:void(0)" data-field="cat_name">时间</a> </li>
                        </ul>
                      </div>
                      <input type="text" class="form-control" value="" name="keyword" placeholder="请输入名称">
                    </div>
                  </form>
                  <div class="toolbar-btn-action">
                    <a class="btn btn-primary m-r-5" href="#!" data-toggle="modal" data-target="#myModal"><i class="mdi mdi-plus" ></i> 新增</a>
                  </div>
                </div>
                <div class="card-body">

                  <div class="table-responsive">
                    <table class="table table-bordered">
                      <thead>
                      <tr>

                        <th>编号</th>
                        <th>农产品分类</th>
                      </tr>
                      </thead>
                      <tbody id="tb_data">
                      </tbody>
                    </table>
                  </div>


                  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                          <h4 class="modal-title" id="myModalLabel">新增分类</h4>
                        </div>
                        <div class="modal-body">
                          <label class="col-xs-12" for="example-text-input">分类名称</label>
                          <input class="form-control" type="text" id="example-text-input" name="example-text-input" placeholder="请输入名称..">
                         </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                          <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="addClassifyItem()">点击新增</button>
                        </div>
                      </div>
                    </div>
                  </div>
                  <%--                分页，先不实现--%>
                  <%--                <ul class="pagination">--%>
                  <%--                  <li class="disabled"><span>«</span></li>--%>
                  <%--                  <li class="active"><span>1</span></li>--%>
                  <%--                  <li><a href="#1">2</a></li>--%>
                  <%--                  <li><a href="#1">3</a></li>--%>
                  <%--                  <li><a href="#1">4</a></li>--%>
                  <%--                  <li><a href="#1">5</a></li>--%>
                  <%--                  <li><a href="#1">6</a></li>--%>
                  <%--                  <li><a href="#1">7</a></li>--%>
                  <%--                  <li><a href="#1">8</a></li>--%>
                  <%--                  <li class="disabled"><span>...</span></li>--%>
                  <%--                  <li><a href="#!">14452</a></li>--%>
                  <%--                  <li><a href="#!">14453</a></li>--%>
                  <%--                  <li><a href="#!">»</a></li>--%>
                  <%--                </ul>--%>

                </div>
              </div>
            </div>

          </div>

        </div>

      </main>
      <!--End 页面主要内容-->
  </div>
</div>


<script type="text/javascript">

  function queryAllClassify() {
    $.ajax({
      type: "get",
      async: true,
      url: serverUrl + "/types/queryTypes",
      dataType: "json",
      xhrFields: {
        withCredentials: true
      },
      success: function (res) {
        if (res.success) {
          //请求成功
          var resData = res.data;
          for (let i = 0; i < resData.length; i++) {
            var item = resData[i];

            $("#tb_data").append("<tr>\n" +
                    "                      <td id=\"" + item.tsId + "\">" + item.tsId + "</td>\n" +
                    "                      <td>" + item.tsName + "</td>\n" +
                    "                    </tr>");

          }
        } else {
          alert(res.msg);
        }

      },
      error: function (res) {
        alert("请求出错");
      }
    });
  }

  //加载页面请求
  queryAllClassify();

  /**
   * 新增功能
   * @param cyId
   */
  function addClassifyItem() {
    //修改
    var tsName = $("#example-text-input").val();
    if (tsName == null) {
      alert("标题或内容不能为空");
      return;
    }
    $.ajax({
      type: "POST",
      contentType: "application/json;charset=utf-8",
      xhrFields: {
        withCredentials: true
      },
      url: serverUrl + "/types/addTypes",
      data: JSON.stringify({"tsName":tsName}),
      dataType: "json",
      success: function (res) {
        $("#tb_data").empty();
        queryAllClassify();
      },
      error: function (res) {
        alert("请求错误");
      }
    });
    $("#example-text-input").val("");
  }

  /**
   * 修改指定的简介
   */
  function upDataClassifyItem(element) {
    //修改的id号
    var cyId = element.getAttribute("id");
    console.log("修改简介为:" + cyId);
  }

  /**
   * 删除选中的item
   */
  function deleteClassifyItem(element) {
    //修改的id号
    var tsId = element.getAttribute("id");
    $.ajax({
      type: "GET",
      url: serverUrl + "/types/deleteTypes?TsId=" + tsId,
      xhrFields: {
        withCredentials: true
      },
      dataType: "json",
      success: function (res) {
        if (res.success) {
          //清楚表，数据然后，调用ajax请求查询一次新数据
          $("#tb_data").empty();
          queryAllClassify();
        }
      },
      error: function (res) {
        alert("请求出错");
      }
    });


  }

  /**
   * 查看单条新闻
   */
  function queryClassifyById(element) {
    var nkId = element.getAttribute("id");

  }
</script>
</body>
</html>
